<?php

namespace Admin\Controller;

class RoleController extends AdminController
{
    public function index()
    {
        A('Public')->sendMsg();
        $this->display();
    }

    public function view()
    {
        $n = D("Role");
        $node = D("Node");
        import("ORG.Util.Page"); //导入分页类
        $page = $_POST["page"];
        $rows = $_POST["rows"];
        $sort = $_POST["sort"];
        $order = $_POST["order"];
        $count = $n->count(); //计算总数
        //$p = new Page($count,$rows);order($sort+','+$order)->
        $list = $n->relation(true)->order('id desc')->select();
        //$list = $n->select();
        foreach ($list as $key => $val) {
            $pieces = explode(",", rtrim($val['rules'], ','));
            foreach ($pieces as $k => $v) {
                $nodelist = $node->where('id=' . $v)->select();
                $list[$key]['nodes'][$k] = $nodelist[0];
            }
        }
        $list = json_encode($list);
        $result = '{"total":' . $count . ',"rows":' . $list . '}';
        echo($result); //输出json数据
    }

    public function viewlist()
    {
        $n = D("Role");
        $node = D("Node");
        import("ORG.Util.Page"); //导入分页类
        $page = $_POST["page"];
        $rows = $_POST["rows"];
        $sort = $_POST["sort"];
        $order = $_POST["order"];
        $count = $n->count(); //计算总数
        //$p = new Page($count,$rows);order($sort+','+$order)->
        $list = $n->relation(true)->order('id desc')->select();
        //$list = $n->select();
        foreach ($list as $key => $val) {
            $pieces = explode(",", rtrim($val['rules'], ','));
            foreach ($pieces as $k => $v) {
                $nodelist = $node->where('id=' . $v)->select();
                $list[$key]['nodes'][$k] = $nodelist[0];
            }
        }
        $list = json_encode($list);
        $result = '{"total":' . $count . ',"rows":' . $list . '}';
        return ($result); //输出json数据
    }

    //添加
    public function add()
    {
        if (empty($_POST)) {
            $Node = A('Node');
            $nodelist = $Node->viewlist();
            $this->assign('nodelist', $nodelist);
            $this->display();
        } else {
            $m = D("Role");
            if ($vo = $m->create()) {
                if ($m->add($vo)) {
                    $this->success('添加用户成功！', 1);
                } else {
                    $this->error('添加失败！', 0);
                }
            } else {
                $this->error($m->getError(), 3);
            }
        }
    }

    // 更新数据
    public function edit()
    {
        if (empty($_POST)) {
            $Node = A('Node');
            $nodelist = $Node->viewlist();
            $this->assign('nodelist', $nodelist);
            $this->display();
        } else {
            $Form = D("Role");
            if ($vo = $Form->create()) {
                if ($Form->save($vo)) {
                    $this->success('更新成功！', 1);
                } else {
                    $this->error('没有更新任何数据!', 0);
                }
            } else {
                $this->error($Form->getError(), 0);
            }
        }
    }

    // 删除数据
    public function delete()
    {
        if (!empty($_GET['ids'])) {
            $d = D("Role");
            $condition['id'] = array('in', $_GET['ids']);
            $result = $d->where($condition)->delete();
            if (false !== $result) {
                $this->success('删除成功！', 1);
            } else {
                $this->error($d->getError(), 0);
            }
        } else {
            $this->error('删除项不存在！' . $d->getError(), 3);
        }
    }
}
